package com.gadgets.cm01.role.service.impl;

import com.gadgets.cm01.auth.pojo.Authority;
import com.gadgets.cm01.role.dto.RoleAuthAllocationDTO;
import com.gadgets.cm01.role.dto.RoleAuthQueryDTO;
import com.gadgets.cm01.role.pojo.RoleAuthorityRelation;
import com.gadgets.cm01.role.repository.IRoleAuthorityRelationMapper;
import com.gadgets.cm01.role.service.IRoleAuthAllocationService;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.PaginationUtil;
import com.gadgets.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-10-10
 * 
 */
@Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
@Service
public class RoleAuthAllocationServiceImpl implements IRoleAuthAllocationService {

    @Autowired
    private IRoleAuthorityRelationMapper roleAuthorityRelationMapper;

    /**
     * 分配菜单权限给用户
     *
     * @param authAllocationDTO 分配信息
     *
     * @throws GadgetsServiceException 服务异常
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
    @Override
    public void allocationRoleAuth(RoleAuthAllocationDTO authAllocationDTO) throws GadgetsServiceException {
        Long[] authIds = StringUtils.splitStrToLong(authAllocationDTO.getAuthIds());
        for (Long authId : authIds) {
            RoleAuthorityRelation relation = new RoleAuthorityRelation();
            relation.setAuthId(authId);
            relation.setRoleId(authAllocationDTO.getRoleId());
            roleAuthorityRelationMapper.insertRoleAuthRelationByBatch(relation);
        }
    }

    /**
     * 撤销菜单权限对用户的分配
     * 撤销菜单权限对用户的分配
     * 撤销菜单权限对用户的分配
     *
     * @param authAllocationDTO 分配信息
     *
     * @throws GadgetsServiceException 服务异常
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
    @Override
    public void reAllocationRoleAuth(RoleAuthAllocationDTO authAllocationDTO) throws GadgetsServiceException {
        Long[] authIds = StringUtils.splitStrToLong(authAllocationDTO.getAuthIds());
        // 如果累计的菜单树大于200个，考虑SQL超长和IN超长的问题，需要分段删除，每段200个，如果不足200个，则只会循环一次
        int pageTm = 1;
        int startIndex = 0;
        int endIndex = PaginationUtil.getPageEndNum(authIds.length,pageTm++,200);
        boolean endFlag = false;
        while (!endFlag) {
            authAllocationDTO.setAuthIdArr(Arrays.copyOfRange(authIds,startIndex,endIndex));
            if(endIndex == authIds.length){
                endFlag = true;
            }
            startIndex = endIndex;
            endIndex = PaginationUtil.getPageEndNum(authIds.length,pageTm++,200);
            roleAuthorityRelationMapper.deleteRoleAuthRelationByBatch(authAllocationDTO);
        }
    }

    /**
     * 查询菜单-角色映射关系信息
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsServiceException 服务异常
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class, propagation = Propagation.SUPPORTS)
    @Override
    public List<RoleAuthorityRelation> queryAuthRoleByCondition(RoleAuthQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<RoleAuthorityRelation> list = roleAuthorityRelationMapper.selectRoleAuthRelationByCondition(queryDTO);
        PaginationUtil.getPaginationResultInfo(list,pagination);
        return list;
    }

    /**
     * 分页条件查询菜单李彪
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsServiceException 服务异常
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class, propagation = Propagation.SUPPORTS)
    @Override
    public List<Authority> queryRoleAuthInfoByCondition(RoleAuthQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<Authority> authorities = roleAuthorityRelationMapper.selectRoleAuthInfoByCondition(queryDTO);
        PaginationUtil.getPaginationResultInfo(authorities,pagination);
        return authorities;
    }

}
